package leetcode;

public class a572另一颗树的子树 {
    public static boolean isSubtree(TreeNode root, TreeNode subRoot) {
        if (root==null&&subRoot==null) return  true;
        if (root==null||subRoot==null) return false;
        return issubtree(root,subRoot)||isSubtree(root.left,subRoot)||isSubtree(root.right,subRoot);

    }
    public static boolean issubtree(TreeNode root,TreeNode subRoot){
        if (root==null&&subRoot==null) return  true;
        if (root==null||subRoot==null) return false;
        return root.val==subRoot.val&&issubtree(root.left,subRoot.left)
                &&issubtree(root.right,subRoot.right);
    }

    public static void main(String[] args) {
        TreeNode root = new TreeNode(3); // 创建根节点，值为 3

// 创建左子树
        TreeNode leftChild1 = new TreeNode(4); // 创建左子节点，值为 4
        TreeNode leftChild2 = new TreeNode(5); // 创建右子节点，值为 5
        root.left = leftChild1;
        leftChild1.right = leftChild2;

// 创建右子树
        TreeNode rightChild1 = new TreeNode(1); // 创建左子节点，值为 1
        TreeNode rightChild2 = new TreeNode(2); // 创建右子节点，值为 2
        root.right = rightChild1;
        rightChild1.right = rightChild2;


        TreeNode root1 = new TreeNode(4); // 创建根节点，值为 4
        TreeNode leftChild = new TreeNode(1); // 创建左子节点，值为 1
        TreeNode rightChild = new TreeNode(2); // 创建右子节点，值为 2

// 将左右子节点连接到根节点
        root1.left = leftChild;
        root1.right = rightChild;


        System.out.println(isSubtree(root,root1));



    }
}
